/* 
 * File:   timer.c
 * Author: Marcel
 *
 * Created on 3. September 2012, 14:30
 */

#include <p18f2620.h>
#include <timers.h>
#include <uart.h>
#include <stdlib.h>
#include "vfd.h"
#include "timer.h"

volatile unsigned char timer_counter_500ms = 0, timer_counter_1s = 0,
                       timer_2ms_flag = 0, timer_500ms_flag = 0, timer_1s_flag = 0,
                       timer_flag = 0;
volatile unsigned char countdown_run = 0, countdown_state = 1;

void timer_isr(void) {
    WriteTimer0(125);   // Preload timer
    timer_flag = 1;

    timer_2ms_flag = 1;

    if(timer_counter_500ms >= 225) {
        timer_500ms_flag = 1;
        timer_counter_500ms = 0;

        if(timer_counter_1s >= 2) {
            timer_1s_flag = 1;
            timer_counter_1s = 0;
        }
        timer_counter_1s++;
    }
    timer_counter_500ms++;

    if(countdown_run)
        countdown_state = timer_countdown();
}

void timer_flags(void) {
//    if(timer_2ms_flag)
//        timer_2ms_flag = 0;

    if(timer_500ms_flag)
        timer_500ms_flag = 0;

//    if(timer_1s_flag)
//        timer_1s_flag = 0;

    if(timer_flag) {
        timer_flag = 0;
    }
}

unsigned char timer_2ms(void) {
    return timer_2ms_flag?1:0;
}

unsigned char timer_500ms(void) {
    return timer_500ms_flag?1:0;
}

unsigned char timer_1s(void) {
    return timer_1s_flag?1:0;
}

unsigned char hr=0, min=0, sec=0, done=0;
void timer_countdown_init(unsigned char h, unsigned char m, unsigned char s) {
    if(done == 0) {
        hr = h;
        min = m;
        sec = s;
        done = 1;
    }
}

void timer_countdown_start(void) {
    countdown_run = 1;
}

void timer_countdown_stop(void) {
    countdown_run = 0;
}

unsigned char timer_countdown(void) {
    if(timer_1s_flag) {
        LATC ^= 0b00000100;
        // Calculation
        if(sec <= 0) {
            if((min <= 0) && (hr <= 0)) {
                done = 0;
                return 0;   // Countdown finished
            }
            sec = 60;
            if(min > 0) {
                min--;
            }
            else {
                min = 59;
                sec = 59;
                hr--;
                return 2;   // One hour over
            }
        }
        sec--;
        timer_1s_flag = 0;
    }
    return 1;   // Countdown not finished yet
}

void timer_putcountdown(void) {
    char hr_str[4], min_str[4], sec_str[4];

    // VFD Output
    btoa(hr, hr_str);
    btoa(min, min_str);
    btoa(sec, sec_str);
    if(hr<10)
        vfd_putc((const rom char) '0');
    vfd_puts(hr_str);
    vfd_putc((const rom char)':');
    if(min<10)
        vfd_putc((const rom char) '0');
    vfd_puts(min_str);
    vfd_putc((const rom char)':');
    if(sec<10)
        vfd_putc((const rom char) '0');
    vfd_puts(sec_str);
}

void timer_reset_countdown(void) {
    done = 0;
}


void timer_init(void) {
    OpenTimer0(TIMER_INT_ON &
               T0_8BIT &
               T0_SOURCE_INT &
               T0_PS_1_32);

    INTCON2bits.TMR0IP = 1;
    // Preload timer with 49911, so the timer frequency is 500 Hz = 2ms
    WriteTimer0(125);
}